/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package store;

import java.awt.Point;
import java.util.ArrayList;
import model.hitoModel;
import model.roadModel;

/**
 *
 * @author JorgeLuis
 */
public class graphStore {
    
    private ArrayList<hitoModel> hitos  = new ArrayList<hitoModel>();
    private ArrayList<roadModel> roads  = new ArrayList<roadModel>();

    public graphStore() {
    }

    public ArrayList<hitoModel> getHitos() {
        return hitos;
    }

    public ArrayList<roadModel> getRoads() {
        return roads;
    }

    public void setHitos(ArrayList<hitoModel> hitos) {
        this.hitos = hitos;
    }

    public void setRoads(ArrayList<roadModel> roads) {
        this.roads = roads;
    }
    
    public void addHito(hitoModel newHito){
        hitos.add(newHito);
    }
    
    public void addRoad(roadModel newRoad){
        roads.add(newRoad);
    }

    public hitoModel findHito(Point point) {
        
        for (int i = 0; i < hitos.size(); i++) {
            
            hitoModel hito = hitos.get(i);
            if(hitos.get(i).intersects(point)){
                
                return hitos.get(i);
            }
        }
        return null;
    }
    
    public roadModel findRoad(Point point ) {
        
        for (int i = 0; i < roads.size(); i++) {
            
            roadModel road = roads.get(i);
            if(road.intersects(point)){
                return roads.get(i);
            }
            
        }
        return null;
    }

    public void removeRoad(roadModel road) {
        
        if(road.getHitoOne() != null){
            road.getHitoOne().removeRoad(road.getId());
        }
        if(road.getHitoTwo() != null){
            road.getHitoTwo().removeRoad(road.getId());
        }
        System.out.println("camino eliminado id: "+road.getId());
        this.roads.remove(road);
    }
    
    public void removeHito(hitoModel hito){
        
        int lenRoads = hito.getRoads().size();
        
        for (int i = 0; i < lenRoads; i++) {
            
            this.removeRoad(hito.getRoads().get(0));
        }
        System.out.println("hito eliminado id: "+hito.getId());
        this.hitos.remove(hito);
    }

    public hitoModel getHito(int id) {
        
        int size = hitos.size();
        for (int i = 0; i < size; i++) {
            
            if(hitos.get(i).getId() == id){
                return hitos.get(i);
            }
            
        }
        return null;
    }
    
    public roadModel getRoad(int id) {
        
        int size = hitos.size();
        for (int i = 0; i < size; i++) {
            
            if(roads.get(i).getId() == id){
                return roads.get(i);
            }
            
        }
        return null;
    }
    
}
